package com.efounder.frame.fragment.attendance;

import android.net.http.SslError;
import android.os.Bundle;
import android.os.Environment;
import androidx.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.GeolocationPermissions;
import android.webkit.JavascriptInterface;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.efounder.constant.EnvironmentVariable;
import com.efounder.frame.baseui.EFFragment;
import com.efounder.frame.utils.Constants;
import com.efounder.mobilemanager.R;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by zhangshunyun on 2017/5/18.
 * <p>
 * 考勤应用号统计Fragment
 */

public class EFStatisticsFragment extends EFFragment {

    private WebView mWebView;
    private String userId;
    private String nickName;
    private String userName;
    private String passWord;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.ef_tj_attendance, container, false);
        mWebView = (WebView) view.findViewById(R.id.tj_webview);
        mWebView.clearCache(true);
        mWebView.setWebViewClient(new WebViewClient() { //对https支持
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }
        });
        getInformation();
        //允许JavaScript执行
        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        //webview定位相关设置  
        settings.setGeolocationEnabled(true);
        settings.setDomStorageEnabled(true);
        mWebView.setVerticalScrollBarEnabled(false);//垂直不显示滚动条   
        mWebView.addJavascriptInterface(new JavaScriptInterface(), "OSPPlugin");
        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
                callback.invoke(origin, true, false);
                super.onGeolocationPermissionsShowPrompt(origin, callback);
            }
        });
        mWebView.loadUrl("file://" + Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "OSPMobileLiveApp" + "/" + userId + "/Apps/3289/html/EF_Mine.html");
        return view;
    }

    private void getInformation() {
        //132
        userId = EnvironmentVariable.getProperty(Constants.CHAT_USER_ID);
        nickName = EnvironmentVariable.getProperty(Constants.KEY_NICK_NAME);
        userName = EnvironmentVariable.getProperty(Constants.CHAT_USER_ID);
        passWord = EnvironmentVariable.getProperty(Constants.CHAT_PASSWORD);
    }

    public class JavaScriptInterface {

        public JavaScriptInterface() {
        }

        @JavascriptInterface
        public String getUserInfo() throws JSONException {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("userName", userName);
            jsonObject.put("passWord", passWord);
            jsonObject.put("IMUserName", userId);
            jsonObject.put("IMPassWord", passWord);
            jsonObject.put("nickName", nickName);
            return jsonObject.toString();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mWebView.clearCache(true);
    }
}
